package com.lc.recovery.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.EditText;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.base.BaseDialog;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.GetCodeApi;
import com.lc.recovery.http.response.PublicMsg;
import com.lc.recovery.ui.dialog.MessageDialog;
import com.lc.widget.view.ClearEditText;
import com.lc.widget.view.CountdownView;
import com.lc.widget.view.PasswordEditText;
import com.lc.widget.view.RegexEditText;

import androidx.appcompat.widget.AppCompatImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 注册界面
 */
public final class RegisterActivity extends AppActivity {


    @BindView(R.id.iv_login_logo)
    AppCompatImageView ivLoginLogo;
    @BindView(R.id.et_register_phone)
    ClearEditText etRegisterPhone;
    @BindView(R.id.et_login_code)
    RegexEditText etLoginCode;
    @BindView(R.id.cv_phone_reset_countdown)
    CountdownView cvPhoneResetCountdown;
    @BindView(R.id.et_register_password)
    PasswordEditText etRegisterPassword;
    @BindView(R.id.et_register_name)
    EditText etRegisterName;
    @BindView(R.id.check_tongyi)
    CheckBox checkTongyi;

    @Override
    protected int getLayoutId() {
        return R.layout.register_activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.cv_phone_reset_countdown, R.id.tv_xieyi, R.id.yinsi, R.id.btn_register_commit, R.id.tv_password_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cv_phone_reset_countdown:
                if (etRegisterPhone.getText().toString().length() != 11) {
                    etRegisterPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                    toast(R.string.common_phone_input_error);

                    return;
                }



                // 隐藏软键盘
                hideKeyboard(getCurrentFocus());
                // 获取验证码
                EasyHttp.post(this)
                        .api(new GetCodeApi()
                                .setMobile(etRegisterPhone.getText().toString()).setType("1"))
                        .request(new HttpCallback<PublicMsg>(this) {

                            @Override
                            public void onSucceed(PublicMsg data) {
                                if (data.getCode()==200) {
                                    toast(R.string.common_code_send_hint);
                                    cvPhoneResetCountdown.start();
                                }else if (data.getCode()==-2){

                                    // 消息对话框
                                    new MessageDialog.Builder(getActivity())
                                            // 标题可以不用填写
                                            .setTitle("该账号已存在")
                                            // 内容必须要填写
                                            .setMessage("若确认是您本人注册，可直接登陆")
                                            // 确定按钮文本
                                            .setConfirm("立即登录")
                                            // 设置 null 表示不显示取消按钮
                                            .setCancel(getString(R.string.common_cancel))
                                            // 设置点击按钮后不关闭对话框
                                            //.setAutoDismiss(false)
                                            .setListener(new MessageDialog.OnListener() {

                                                @Override
                                                public void onConfirm(BaseDialog dialog) {
                                                    startActivity(LoginPassActivity.class);
                                                }

                                                @Override
                                                public void onCancel(BaseDialog dialog) {

                                                    finish();
                                                }
                                            })
                                            .show();
                                }else {
                                    toast(data.getMsg());
                                }
                            }
                            @Override
                            public void onFail(Exception e) {
//                                super.onFail(e);

                            }
                        });




                break;
            case R.id.tv_xieyi:
                startActivity(new Intent(this,RechargeActivity.class).putExtra("title","用户协议").putExtra("Url","https://hyq.hljjrysm.com/xieyi.html"));
                break;
            case R.id.yinsi:
                startActivity(new Intent(this,RechargeActivity.class).putExtra("title","隐私条款").putExtra("Url","https://hyq.hljjrysm.com/yinsi.html"));
                break;
            case R.id.btn_register_commit:
                if (!checkTongyi.isChecked()) {
                    toast("请同意《用户协议》及《隐私条款》");
                    return;
                }
                  if (etRegisterPhone.getText().toString().isEmpty()){
                      toast("手机号不能为空");
                      return;
                  }
                if (etRegisterPassword.getText().toString().isEmpty()){
                    toast("登录密码不能为空");
                    return;
                }
                if (etLoginCode.getText().toString().isEmpty()){
                    toast("验证码不能为空");
                    return;
                }
//                if (etRegisterName.getText().toString().isEmpty()){
//                    toast("回收员分享码不能为空");
//                    return;
//                }

                startActivity(new Intent(this,IdentitySelectionActivity.class).putExtra("mobile",etRegisterPhone.getText().toString()).putExtra("password",etRegisterPassword.getText().toString()).putExtra("code",etLoginCode.getText().toString()).putExtra("share_code",etRegisterName.getText().toString()));
                finish();


                break;
            case R.id.tv_password_login:
                startActivity(LoginPassActivity.class);
                break;
        }
    }
}